package com.api.shopapi.mapper;

import com.api.shopapi.entity.VolunteerTeam;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface VolunteerTeamMapper {

    @Insert("insert into volunteer_team " +
            "(" +
            "VolunteerTeamCode," +
            "UserCode," +
            "Address,"+
            "ApplyStatus," +
            "CreateTime," +
            "VolunteerCount," +
            "CommandCenterCode," +
            "GovernmentCode" +
            ") " +
            "values " +
            "(" +
            "#{volunteerTeam.VolunteerTeamCode}," +
            "#{volunteerTeam.UserCode}," +
            "#{volunteerTeam.Address}," +
            "#{volunteerTeam.ApplyStatus}," +
            "#{volunteerTeam.CreateTime}," +
            "#{volunteerTeam.VolunteerCount}," +
            "#{volunteerTeam.CommandCenterCode}," +
            "#{volunteerTeam.GovernmentCode}" +
            ")" )
    int insert(@Param("volunteerTeam") VolunteerTeam volunteerTeam);


    @Delete("delete from volunteer_team where volunteerTeamCode=#{volunteerTeamCode}")
    int delete(@Param("volunteerTeamCode") String volunteerTeamCode);


    @Update("update volunteer_team set " +
            " Address=#{volunteerTeam.Address}," +
            " ApplyStatus=#{volunteerTeam.ApplyStatus}," +
            " ApplyTime=#{volunteerTeam.ApplyTime}," +
            " PassTime=#{volunteerTeam.PassTime}," +
            " RejectMessage=#{volunteerTeam.RejectMessage}," +
            " VolunteerCount=#{volunteerTeam.VolunteerCount}" +
            " where volunteerTeamCode=#{volunteerTeam.volunteerTeamCode}")
    int update(@Param("volunteerTeam") VolunteerTeam volunteerTeam);


    @Select("select * from volunteer_team where volunteerTeamCode=#{volunteerTeamCode} limit 1")
    VolunteerTeam getByVolunteerTeamCode(@Param("volunteerTeamCode") String volunteerTeamCode);


    @Select("select * from volunteer_team where userCode=#{userCode} limit 1")
    VolunteerTeam getByUserCode(@Param("userCode") String userCode);

    @Select("<script>" +
            "select * from volunteer_team vt join user u on vt.UserCode=u.UserCode" +
            " where vt.commandCenterCode=#{commandCenterCode} " +
            "<if test ='applyStatus != -1 '>" +
            " and vt.applyStatus = #{applyStatus} " +
            "</if>" +
            " order by vt.CreateTime desc" +
            "</script>")
    List<VolunteerTeam> listByCommandCenterCode(@Param("commandCenterCode") String commandCenterCode,
                                             @Param("applyStatus") int applyStatus);


    @Select("select * from volunteer_team where commandCenterCode=#{commandCenterCode} and ApplyStatus = 1 " +
            " order by ApplyTime desc")
    List<VolunteerTeam> listByCommandCenterCodeApply(@Param("commandCenterCode") String commandCenterCode);


    @Select("<script>" +
            "select * from volunteer_team " +
            " where commandCenterCode in (select commandCenterCode from command_center where governmentCode=#{governmentCode}) " +
            " order by CreateTime desc" +
            "</script>")
    List<VolunteerTeam> listByGovCode(@Param("governmentCode") String governmentCode);

}
